package com.yuqing.sentimentapp.utils;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.yuqing.sentimentapp.QYApplication;

/**
 * Created by shenhe on 2019/5/10.
 */
public class AppUtil {
    private static String imei = "";

    /**
     * 获取手机IMEI号
     */
    public static String getIMEI() {
        if (!TextUtils.isEmpty(imei) && !"000000000000000".equals(imei)) {
            return imei;
        }
        try {
            TelephonyManager telephonyManager = (TelephonyManager) QYApplication.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager.getDeviceId() == null || telephonyManager.getDeviceId().equals("")) {
                // 获取 ANDROID_ID
                String android_id = Settings.System.getString(
                        QYApplication.getAppContext().getContentResolver(), Settings.Secure.ANDROID_ID);
                if (!TextUtils.isEmpty(android_id)) {
                    // 通过 ANDROID_ID 生成 guid（唯一标识符）
                    imei = android_id;
                } else {
                    imei = "000000000000000";
                }
            } else {
                imei = telephonyManager.getDeviceId();
            }
        } catch (Exception e) {
            imei = "000000000000000";
        } finally {
            return imei;
        }
    }
}
